package com.kartoshka.painters 
{
	import flash.display.Graphics;

	import com.kartoshka.painters.NullPainter;

	/**
	 * @author Alexander Zats, 2009
	 */
	public class BorderSolidPainter extends NullPainter 
	{

		private var _color : Number;
		private var _alpha : Number;
		private var _thickness : Number;
		protected var _lineType : String;

		public function BorderSolidPainter( thickness : Number = 1, color : uint = 0x000000, alpha : Number = 1, lineType : String = "inside" )
		{
			super( );
			_color = color;
			this.alpha = alpha;
			this.lineType = lineType;
			this.thickness = thickness; 
		}

		override public function draw(graphics : Graphics, width : Number, height : Number) : void
		{
			graphics.beginFill( _color, _alpha );
			
			var doubleThickness : Number = _thickness * 2;
			var halfThickness : Number = _thickness / 2;
			if ( cornerRadius )
			{
				switch ( _lineType )
				{
					case "outside":
						graphics.drawRoundRect( - _thickness, - _thickness, width + doubleThickness, height + doubleThickness, cornerRadius + _thickness, cornerRadius + _thickness );
						graphics.drawRoundRect( 0, 0, width, height, cornerRadius, cornerRadius );
						break;
					case "inside":
						graphics.drawRoundRect( 0, 0, width, height, cornerRadius, cornerRadius );
						graphics.drawRoundRect( _thickness, _thickness, width - doubleThickness, height - doubleThickness, cornerRadius - _thickness, cornerRadius - _thickness );
						break;
					default:
						graphics.drawRoundRect( - halfThickness, - halfThickness, width + _thickness, height + _thickness, cornerRadius + halfThickness, cornerRadius + halfThickness );
						graphics.drawRoundRect( halfThickness, halfThickness, width - _thickness, height - _thickness, cornerRadius - halfThickness, cornerRadius - halfThickness );
						break;
				}
			}
			else
			{
				switch ( _lineType )
				{
					case "outside":
						graphics.drawRect( - _thickness, - _thickness, width + doubleThickness, height + doubleThickness );
						graphics.drawRect( 0, 0, width, height );
						break;
					case "inside":
						graphics.drawRect( 0, 0, width, height );
						graphics.drawRect( _thickness, _thickness, width - doubleThickness, height - doubleThickness );
						break;
					default:
						graphics.drawRect( - halfThickness, - halfThickness, width + _thickness, height + _thickness );
						graphics.drawRect( halfThickness, halfThickness, width - _thickness, height - _thickness );
						break;
				}
			}
			
			graphics.endFill( );
		}

		public function set color( value : Number ) : void
		{
			if ( value == _color ) return;
			_color = value;
		}

		public function get color() : Number
		{
			return _color;
		}

		public function set alpha( value : Number ) : void
		{
			if ( value == _alpha ) return;
			if ( value > 1 ) _alpha = 1;
			else if ( value < 0 ) _alpha = 0;
			else _alpha = value;
		}

		public function get alpha() : Number
		{
			return _alpha;
		}

		public function set lineType( value : String ) : void
		{
			if ( value == _lineType ) return;
			if ( value == "outside" ) _lineType = "outside";
			else if ( value == "inside" ) _lineType = "inside";
			else _lineType = "center"; 
		}

		public function get lineType() : String
		{
			return _lineType;
		}

		public function set thickness( value : Number ) : void
		{
			if ( value == _thickness ) return;
			if ( value < 0 ) _thickness = 0;
			else _thickness = value;
		}

		public function get thickness() : Number
		{
			return _thickness;
		}
	}
}
